var express = require('express');
var router = express.Router();
var resultJson = require('../result-json');
var PostModel = require('../models/Post');
var CommentModel = require('../models/Comment');

var checkLogin = require('../middlewares/check').checkLogin;

router.post('/create.json', checkLogin, function(req, res, next){
    var post = req.body;
    post.author = req.session.user._id;
    post.pv = 0;

    PostModel.create(post)
        .then(function (result) {

            res.send(resultJson(0, result.ops[0]));

        })
        .catch(next);

});
router.post('/edit.json', checkLogin, function(req, res, next){
    var post = req.body;

    var postId = post._id;
    var author = req.session.user._id;
    var ops = {
        title: post.title,
        tags: post.tags,
        cover: post.cover,
        abstract: post.abstract,
        content: post.content
    }
    PostModel.edit(postId, author, ops)
        .then(function () {

            res.send(resultJson(0));

        })
        .catch(next);

});
router.post('/remove.json', checkLogin, function(req, res, next){

    var postId = req.body.postId;
    var author = req.session.user._id;

    PostModel.removePost(postId, author)
        .then(function () {
            res.send(resultJson(0));
        })
        .catch(next);
});

router.get('/getAllPosts.json', function(req, res, next){
    var ops = {
        start: req.query.start,
        count: req.query.count,
        keywords: req.query.keywords || ''
    }

    PostModel.getPosts(ops)
        .then(function(result){

            res.send(resultJson(0, {
                list: result[0],
                count: result[1]
            }))

        });    
});

router.get('/getFilterPosts.json', function(req, res, next){
    var ops = {
        start: req.query.start,
        count: req.query.count,
        type: req.query.type || ''
    }

    PostModel.getFilterPosts(ops)
        .then(function(result){

            res.send(resultJson(0, {
                list: result
            }))

        });    
});

router.get('/getMyPosts.json', checkLogin, function(req, res, next){
    var author = req.session.user._id;

    var ops = {
        author: author,
        start: req.query.start,
        count: req.query.count
    }

    PostModel.getPosts(ops)
        .then(function(result){

            res.send(resultJson(0, {
                list: result[0],
                count: result[1]
            }))

        });    
});

router.get('/getPostById.json', function(req, res, next){
    var postId = req.query.postId;
    if (!postId){
        return res.send(resultJson(200));
    }
    Promise.all([
        PostModel.getPostById(postId),
        CommentModel.getCommentsByPostId(postId),
        PostModel.addPv(postId)
    ])
    .then(function(result){console.log(result)
        var author = result[0].author._id;

        var canEdit = false;
        if (req.session.user){
            var userId = req.session.user._id;
            canEdit = author == userId ? true : false;
        }

        res.send(resultJson(0, {
            post: result[0],
            comments: result[1],
            canEdit: canEdit
        }));
    });
});

module.exports = router;

